﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using GalaSoft.MvvmLight;
using MusicExplorer.Data;
using MusicExplorer.WebRepository.Service;

namespace MusicExplorer.Wrappers
{
    public class TrackWrapper : ViewModelBase
    {
        private readonly TRACK m_track;

        public TrackWrapper(TRACK track)
        {
            m_track = track;
            m_isRead = m_track.track_is_read;
            m_isFavorite = m_track.track_is_favorite;
        }

        public TRACK Track { get { return m_track; } }

        public string Title
        {
            get
            {
                if (m_track.JN_ARTIST_TRACK.Count == 0)
                    return m_track.track_name;
                else
                {
                    string title = string.Empty;

                    var originals = m_track.JN_ARTIST_TRACK.Where(a => GetTypeArtistFromRef(a.REF_TYPE_ARTIST) == TypeArtist.Original);
                    if(originals != null)
                        title += String.Join(", ", originals.Select(a => a.ARTIST.artist_name));

                    title += " - " + m_track.track_name;

                    return title;
                }
            }
        }

        private TypeArtist GetTypeArtistFromRef(REF_TYPE_ARTIST refTypeArtist)
        {
            switch (refTypeArtist.type_artist_libelle)
            {
                case "Original":
                    return TypeArtist.Original;
                case "Remixer":
                    return TypeArtist.Remixer;
                default:
                    return TypeArtist.Original;
            }
        }

        private bool m_isFavorite;
        public bool IsFavorite
        {
            get { return m_isFavorite; }
            set
            {
                if (m_isFavorite != value)
                {
                    m_isFavorite = value;
                    ServiceWebRepository.SetTrackIsFavorite(Track);
                    RaisePropertyChanged("IsFavorite");
                }
            }
        }

        private bool m_isRead;
        public bool IsRead
        {
            get { return m_isRead; }
            set
            {
                if (m_isRead != value)
                {
                    m_isRead = value;
                    RaisePropertyChanged("IsRead");
                }
            }
        }

        private bool m_isSelected;
        public bool IsSelected
        {
            get { return m_isSelected; }
            set
            {
                if (m_isSelected != value)
                {
                    m_isSelected = value;
                    RaisePropertyChanged("IsSelected");
                }
            }
        }

        public string PublishDate { get { return m_track.track_publish_date.ToString("yyyy/MM/dd"); } }

        public TypeWebRepository GetTypeWebRepository
        {
            get
            {
                return ServiceWebRepository.GetTypeWebRepositoryFromTrack(Track);
            }
        }
    }
}
